#include <QSettings>
#include "SrvConnection.h"
#include "SrvConnectionHttp.h"
#include "SrvConnectionLocal.h"
#include "scImpl.h"

unsigned long SrvConnection::nextTrans = 0;

#define FCT "SrvConnection::createFromSettings"
SrvConnection * SrvConnection::createFromSettings()
{
	QSettings settings;
	if(settings.value("server/isLocal").toBool())
		return new SrvConnectionLocal(settings.value("server/localDb").toString());
	else
		return new SrvConnectionHttp(settings.value("server/host").toString(),
				settings.value("server/port").toInt(), settings.value("server/useHttps").toBool());
}

#undef FCT
#define FCT "SrvConnection::SrvConnection"
SrvConnection::SrvConnection() :
	versMaj(-1), versMin(-1), versBld(-1), verb(false)
{
	ctrc(Trc::SETUP,APP,FCT)<<endl;
}

#undef FCT
#define FCT "SrvConnection::~SrvConnection"
SrvConnection::~SrvConnection()
{
	ctrc(Trc::SETUP,APP,FCT)<<endl;
}

#undef FCT
#define FCT "SrvConnection::createTransactionId"
unsigned long SrvConnection::createTransactionId()
{
	return ++nextTrans;
}

#undef FCT
#define FCT "SrvConnection::errorString"
QString SrvConnection::errorString() const
{
	return localError;
}

#undef FCT
#define FCT "SrvConnection::getVersionInfo"
void SrvConnection::getVersionInfo(QString * versionText, int * major, int * minor, int * build) const
{
	if(versionText)
	{
		if(versMaj>=0)
		{
			ostringstream os;
			os <<"V "<<versMaj<<'.'<<versMin<<'.'<<versBld;
			*versionText=os.str().c_str();
		}
		else
			versionText->clear();
	}
	if(major)
		*major=versMaj;
	if(minor)
		*minor=versMin;
	if(build)
		*build=versMaj;
}
